<?php
namespace Home\Controller;

use Think\Controller;
use Think\Log;

class BaseController extends Controller
{
    const PSIZE = 20;
    const CPAGE = 1;

    public function __construct()
    {
    }

    /**
     * 判断用户是否存在
     * @param $tab
     * @param $user_id
     * @return bool|mixed
     */
    public function checkUser($tab, $user_id)
    {
        $user = M($tab)->where(['id' => $user_id])->find();
        if ($user) {
            return $user;
        } else {
            return false;
        }
    }

    /**
     * 订单表订单状态描述
     * 0=>未下单 1=>待审核 2=>审核未通过 3=>审核通过 4=>上画待验收 5=>下画待验收 6=>订单完成
     * @return array
     */
    public function statusMsg()
    {
        return [
            0=>'未下单',
            1=>'待审核',
            2=>'审核未通过',
            3=>'进行中',
            4=>'上画待验收',
            5=>'上画验收完成',
            6=>'下画待验收',
            7=>'订单完成',
            11=>'高空物料待报价',
            12=>'高空物料报价待审核',
            13=>'高空报价审核失败'
        ];
    }

    /**
     * 统一列表分页数据
     * @param $tab
     * @param string $where
     * @param string $page_size
     * @param string $current_page
     * @param string $field
     * @param string $order
     * @return mixed
     */
    public function getList($tab, $where = [], $page_size = SELF::PSIZE, $current_page = SELF::CPAGE, $field = '*', $order = 'id desc')
    {
        $model = M($tab);
        $list = $model
            ->field($field)
            ->where($where)
            ->order($order)
            ->page("$current_page,$page_size")
            ->select();

        $count = $model->where($where)->count();
        $page_count = ceil($count / $page_size);

        //$ret['data'] = $list;
        //$ret['pages']['count'] = count($list);
        //$ret['pages']['page_count'] = $page_count;
        //$ret['pages']['page_size'] = $page_size;
        //$ret['pages']['current_page'] = $current_page;
        return $list;
    }

    /**
     * 检测用户登陆信息是否正常
     * @param $tab
     * @param $mobile
     * @param $password
     * @return bool|mixed
     */
    public function chenkLogin($tab, $mobile, $password)
    {
        $userInfo = M($tab)->where(['mobile' => $mobile, 'password' => $password])->find();
        if ($userInfo) {
            return $userInfo;
        } else {
            return false;
        }
    }

    /**
     * 根据openid获取用户信息
     * @param $tab
     * @param $openid
     * @return bool|mixed
     */
    public function getInfoByOpenid($tab, $openid)
    {
        $userInfo = M($tab)->where(['openid' => $openid])->find();
        if ($userInfo) {
            return $userInfo;
        } else {
            return false;
        }
    }

    /**
     * 返回对应角色的表明
     * @return array
     */
    public function getRoleTab()
    {
        return [
            1 => 'audit',
            2 => 'store',
            3 => 'worker'
        ];
    }

    /**
     * 判断订单是否下单
     * @param $order_no
     * @return mixed
     */
    public function checkOrder($order_no)
    {
        $orderLog = M('order_status_log')->where(['order_no' => $order_no])->find();
        if ($orderLog) {
            return ture;
        }
        return false;
    }

    /**
     * 检测订单是否提交审核
     * @param $order_no
     * @param $audit_user_id
     * @return bool
     */
    public function checkOrderAudit($order_no, $audit_user_id)
    {
        $order = M('order')->where(['order_no' => $order_no, 'audit_user_id' => $audit_user_id])->find();
        if ($order && $order['order_status'] != 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 通过门店id查询店长所属公司
     * @param $store_id
     * @return string
     */
    public function getStoreCompanyName($store_id)
    {
        $store = M('store')->where(['id' => $store_id])->find();
        $audit = M('audit')->where(['id' => $store['audit_user_id']])->find();
        if ($audit) {
            $company_name = $audit['company_name'];
        } else {
            $company_name = '';
        }
        return $company_name;
    }

    /**
     * 检测订单更新order_ads id
     * @param $order_no
     * @param $ads_id
     * @param $order_ads_id
     * @return bool
     */
    public function checkUpdOrderAdsId($order_no, $ads_id, $order_ads_id)
    {
        $order_ads = M('order_ads')->where(['order_no' => $order_no, 'ads_id' => $ads_id])->find();
        Log::write('更新订单广告，传的id：' . $order_ads_id . '---正确id：' . $order_ads['id']);
        return $order_ads['id'] == $order_ads_id ? true : false;
    }

    /**
     * 检测订单更新order_ads_detail id
     * @param $order_no
     * @param $ads_detail_id
     * @param $order_ads_detail_id
     * @return bool
     */
    public function checkUpdOrderAdsDetailId($order_no, $ads_detail_id, $order_ads_detail_id)
    {
        $order_ads_detail = M('order_ads_detail')->where(['order_no' => $order_no, 'ads_detail_id' => $ads_detail_id])->find();
        Log::write('更新订单广告详情，传的id：' . $order_ads_detail_id . '---正确id：' . $order_ads_detail['id']);
        return $order_ads_detail['id'] == $order_ads_detail_id ? true : false;
    }
}